﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel.Description;

namespace EasyComm.ServiceUnit
{
    [CollectionDataContract]
    public class WorkerServiceUnitCollection : ThreadSafeCollection<WorkerServiceUnit>
    {
        public virtual IEnumerable<ServiceEndpoint> EndpointList
        {
            get
            {
                return GetEndpointList();
            }
        }

        private IEnumerable<ServiceEndpoint> GetEndpointList()
        {
            return GetEndpointList(null);
        }

        public virtual IEnumerable<ServiceEndpoint> GetEndpointList(IComparer<ServiceEndpoint> comparer)
        {
            base.InnerLock.EnterReadLock();
            try
            {
                List<ServiceEndpoint> sortedValues = new List<ServiceEndpoint>();
                foreach (WorkerServiceUnit unit in this)
                {
                    foreach (ServiceContract contract in unit.Contracts)
                    {
                        sortedValues.Add(contract.Endpoint);
                    }
                }
                if (comparer != null)
                {
                    sortedValues.Sort(comparer);
                }
                else
                {
                    sortedValues.Sort();
                }
                return sortedValues;
            }
            finally
            {
                base.InnerLock.ExitReadLock();
            }
        }

        public virtual WorkerServiceUnit this[Uri uri]
        {
            get
            {
                base.InnerLock.EnterReadLock();
                try
                {
                    foreach (WorkerServiceUnit unit in this)
                    {
                        foreach (ServiceContract contract in unit.Contracts)
                        {
                            if (contract.ListenUri == uri)
                            {
                                return unit;
                            }
                        }
                    }
                    return null;
                }
                finally
                {
                    base.InnerLock.ExitReadLock();
                }
            }
        }

        public virtual List<WorkerServiceUnit> this[ServiceContract contract]
        {
            get
            {
                base.InnerLock.EnterReadLock();
                try
                {
                    List<WorkerServiceUnit> unitList = new List<WorkerServiceUnit>();
                    foreach (WorkerServiceUnit unit in this)
                    {
                        foreach (ServiceContract innerContract in unit.Contracts)
                        {
                            if (innerContract.Equals(contract))
                            {
                                unitList.Add(unit);
                            }
                        }
                    }
                    return unitList;
                }
                finally
                {
                    base.InnerLock.ExitReadLock();
                }
            }
        }
    }
}
